const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");

const getUrl = require("./getUrl");

let app = express();

app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use(express.static("../web"));

app.post("/twUrl", async (req, res) => {
  const { url } = req.body;
  try {
    let realUrl = await getUrl(url);
    res.json({ url: realUrl });
  } catch (e) {
    res.json({ url: null });
  }
});

app.listen(3000, () => {
  console.log("server launch ...");
});
